Core Java® Volume I—Fundamentals, Tenth Edition (Lloyd Laird's Library) by Cay S. Horstmann
Author:Cay S. Horstmann
Language: eng
Format: epub
Publisher: Prentice Hall
Published: 2016-03-14T16:00:00+00:00
9.3.1 Basic Map Operations
The Java library supplies two general-purpose implementations for maps: HashMap and TreeMap. Both classes implement the Map interface.
A hash map hashes the keys, and a tree map uses an ordering on the keys to organize them in a search tree. The hash or comparison function is applied only to the keys. The values associated with the keys are not hashed or compared.
Should you choose a hash map or a tree map? As with sets, hashing is usually a bit faster, and it is the preferred choice if you don’t need to visit the keys in sorted order.
Here is how you set up a hash map for storing employees:
Click here to view code image
Map<String, Employee> staff = new HashMap<>(); // HashMap implements Map
Employee harry = new Employee("Harry Hacker");
staff.put("987-98-9996", harry);
...
Whenever you add an object to a map, you must supply a key as well. In our case, the key is a string, and the corresponding value is an Employee object.
To retrieve an object, you must use (and, therefore, remember) the key.
Click here to view code image
String id = "987-98-9996";
e = staff.get(id); // gets harry
If no information is stored in the map with the particular key specified, get returns null.
The null return value can be inconvenient. Sometimes, you have a good default that can be used for keys that are not present in the map. Then use the getOrDefault method.
Click here to view code image
Map<String, Integer> scores = ...;
int score = scores.get(id, 0); // Gets 0 if the id is not present
Keys must be unique. You cannot store two values with the same key. If you call the put method twice with the same key, the second value replaces the first one. In fact, put returns the previous value associated with its key parameter.
The remove method removes an element with a given key from the map. The size method returns the number of entries in the map.
The easiest way of iterating over the keys and values of a map is the forEach method. Provide a lambda expression that receives a key and a value. That expression is invoked for each map entry in turn.
Click here to view code image
scores.forEach((k, v) ->
System.out.println("key=" + k + ", value=" + v));
Listing 9.6 illustrates a map at work. We first add key/value pairs to a map. Then, we remove one key from the map, which removes its associated value as well. Next, we change the value that is associated with a key and call the get method to look up a value. Finally, we iterate through the entry set.
Listing 9.6 map/MapTest.java
Click here to view code image
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12570)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9778)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7071)
Microservices with Go by Alexander Shuiskov(6834)
Practical Design Patterns for Java Developers by Miroslav Wengner(6755)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6696)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6099)
The Art of Crafting User Stories by The Art of Crafting User Stories(5628)
NetSuite for Consultants - Second Edition by Peter Ries(5562)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5368)
Kotlin in Action by Dmitry Jemerov(5062)
